<?php
class TagAction extends Action {
	var $prefix;
	var $userTag;
	function _initialize(){
		$this->prefix = C("DB_PREFIX");
		$this->userTag = D('SpaceTags', 'home');
	}

	public function index(){		
		$type = empty($_REQUEST['type']) ? 'total' : $_REQUEST['type'];
		$countField = $type.'count';
		$field = "tagid, tagname, $countField as tagcount";
		
		$key = t($_POST['key']);
		if(!empty($key)){
			$map['tagname'] = array('like', "%$key%");
		}
		if($type!='total'){
			$map[$countField] = array('exp', '>0');		
		}
		$map['totalcount'] = array('exp', '>0');
		$map['isShow'] = 1;
		
		$list = M('x_tag')->where($map)->field($field)->order("$countField desc")->findpage(200);
		foreach ($list['data'] as $k=>$v){
			if(empty($v['tagname'])){
				unset($list['data'][$k]);
			}
		}
		$this->assign('list', $list);
		
		$this->_rightData();
		$this->setTitle ('热门标签');
		$html = ($type=='user') ? 'user' : 'index';
		$this->display($html);	
	}
	
	function detail(){		
		$tagid = intval($_GET['tagid']);
		if(empty($tagid)){
			$this->error('参数出错');
		}
		$this->setTitle ('热门标签');
		$map['tagid'] = $tagid;
		$list = $this->userTag->where($map)->order('id DESC')->findPage(50);
		$this->assign('list', $list);
	
		$this->_rightData();
		
		$this->display();
	}
	
	function _rightData(){
		$sql = "SELECT tagid, tagname, usercount  FROM ".$this->prefix."x_tag where usercount>0 AND isShow=1 ORDER BY usercount DESC LIMIT 10";
		$topTag  = D('')->query($sql);
		$this->assign('topTag', $topTag);


		$sql = "SELECT a.tagid, a.tagname, a.usercount
			FROM ".$this->prefix."x_tag as a 
			LEFT JOIN ".$this->prefix."space_tag as b ON a.tagid=b.tagid 
			WHERE b.uid=".$this->mid." AND a.isShow=1 LIMIT 10";
		$myTag   = $topTag  = D('')->query($sql);
		$this->assign('myTag', $myTag);
		
		$this->assign('type', $_REQUEST['type']);
	}
	
	function updataCount(){
		D('UserXtag')->allCount();
	}
}
?>